//
//  HRRegestRootView.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRRegestRootView.h"
#import "HREnterTextFiledView.h"
#import "HRVerificationCodeModel.h"
#import "HRValiMobileTool.h"
@interface HRRegestRootView ()
@property (nonatomic, strong)HREnterTextFiledView *phoneTextField;
@property (nonatomic, strong)HREnterTextFiledView *codeTextField;
@property (nonatomic, strong)UIButton *getCodeButton;
@property (nonatomic, strong)HREnterTextFiledView *passWordTextField;
@property(nonatomic, assign)int controlType;

@end
@implementation HRRegestRootView

-(instancetype)initWithFrame:(CGRect)frame andControlType:(int)controlType
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.controlType = controlType;
        
        UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"提示耳机图案"]];
        backImageView.contentMode = UIViewContentModeScaleAspectFill;
        backImageView.clipsToBounds = YES;
        [self addSubview:backImageView];
        backImageView.userInteractionEnabled = YES;

        
        
        self.phoneTextField = [HREnterTextFiledView new];
        [self addSubview:self.phoneTextField];
        self.phoneTextField.textField.placeholder = @"请输入手机号";
        [self.phoneTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.phoneTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
        self.phoneTextField.textField.textColor = [UIColor whiteColor];
        self.phoneTextField.leftImageView.image = [UIImage imageNamed:@"手机"];

        
        
        self.codeTextField = [HREnterTextFiledView new];
        [self addSubview:self.codeTextField];
        self.codeTextField.textField.placeholder = @"请输入验证码";
        [self.codeTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.codeTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
        self.codeTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.codeTextField.textField.textColor = [UIColor whiteColor];
        self.codeTextField.leftImageView.image = [UIImage imageNamed:@"验证码"];

        
        UIButton *getCodeButton = [UIButton buttonWithType:1];
        [getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [getCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        getCodeButton.titleLabel.font = CustomUIFont(14);
        [getCodeButton addTarget:self action:@selector(getCodeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:(self.getCodeButton = getCodeButton)];
        
        
        self.passWordTextField = [HREnterTextFiledView new];
        [self addSubview:self.passWordTextField];
        self.passWordTextField.textField.placeholder = @"请输入密码";
        [self.passWordTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.passWordTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.passWordTextField.textField.textColor = [UIColor whiteColor];
        self.passWordTextField.leftImageView.image = [UIImage imageNamed:@"密码"];
        self.passWordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
        
        UIButton *nextButton = [UIButton buttonWithType:1];
        [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [nextButton setTitleColor:BlueColor forState:UIControlStateNormal];
        nextButton.titleLabel.font = CustomUIFont(17);
        [nextButton addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:nextButton];
        nextButton.layer.cornerRadius = 10;
        nextButton.clipsToBounds = YES;
        nextButton.backgroundColor = [UIColor whiteColor];
        
        
        
        
        
        
        CGFloat Pading = 20;
        WeakSelf
        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];
        
        
        [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf).offset(-60);
            make.left.equalTo(weakSelf).offset(50);
            make.right.equalTo(weakSelf).offset(-50);
            make.height.mas_equalTo(41);
        }];
        
        
        [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.height.equalTo(weakSelf.phoneTextField);
            make.top.equalTo(weakSelf.phoneTextField.mas_bottom).offset(Pading);
            make.right.equalTo(weakSelf.phoneTextField.mas_centerX);
        }];
        
        
        [getCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.codeTextField.mas_right);
            make.top.width.height.equalTo(weakSelf.codeTextField);
        }];
        
       
        
        if (controlType == 2){
            self.passWordTextField.hidden = YES;
            [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(weakSelf.phoneTextField);
                make.top.equalTo(weakSelf.codeTextField.mas_bottom).offset(50);
                make.height.mas_equalTo(45);
            }];
        }else{
            [self.passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.equalTo(weakSelf.phoneTextField);
                make.left.equalTo(weakSelf.phoneTextField);
                make.top.equalTo(weakSelf.codeTextField.mas_bottom).offset(Pading);
            }];
            
            [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(weakSelf.phoneTextField);
                make.top.equalTo(weakSelf.passWordTextField.mas_bottom).offset(50);
                make.height.mas_equalTo(45);
            }];
        }

    }
    
    return self;
}

#pragma -mark- 下一步
- (void)nextButtonClick
{
    self.codeTextField.textField.text = [self.codeTextField.textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (![self.codeTextField.textField.text isEqualToString:self.codeModel.Code])
    {
        [MBProgressHUD showMessage:@"输入的验证码错误" finishBlock:nil];
        return;
    }

    if (self.controlType != 2){
        if (self.passWordTextField.textField.text.length < 6)
        {
            [MBProgressHUD showMessage:@"密码长度不能小于6个字符" finishBlock:nil];
            return;
        }
        if (self.passWordTextField.textField.text.length > 20)
        {
            [MBProgressHUD showMessage:@"密码长度不能大于20个字符" finishBlock:nil];
            return;
        }
    }
    self.passWordTextField.textField.text = [self.passWordTextField.textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.codeModel.password = [self.passWordTextField.textField.text md5String];
    if (self.nextButtonClickBlock)
    {
        self.nextButtonClickBlock();
    }
}

- (void)getCodeButtonClick
{
    if (![HRValiMobileTool valiMobile:self.phoneTextField.textField.text]){
        [MBProgressHUD showMessage:@"输入的手机号无效" finishBlock:nil];
        return ;
    }
    if (self.getCodeButtonClickBlock)
    {
        self.getCodeButtonClickBlock(self.phoneTextField.textField.text);
    }
}

-(void)setCodeModel:(HRVerificationCodeModel *)codeModel
{
    _codeModel = codeModel;
    DDLog(@"验证码==================%@",codeModel.Code);
}




- (void)startCountWhenClickVerRequest{
    [self countDownWithTime:120 countDownBlock:^(int timeLeft) {
        int seconds = timeLeft % 120;
        NSString *strTime = [[NSString stringWithFormat:@"%.2ds",seconds] copy];
        [self.getCodeButton setTitle:strTime forState:UIControlStateNormal];
        self.getCodeButton.enabled = NO;
    } endBlock:^(void){
        [self.getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getCodeButton.enabled = YES;
    }];
}
- (void)countDownWithTime:(int)time
           countDownBlock:(void (^)(int timeLeft))countDownBlock
                 endBlock:(void (^)())endBlock{
    __block int timeout = time; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (endBlock) {
                    endBlock();
                }
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                timeout--;
                if (countDownBlock) {
                    countDownBlock(timeout);
                }
            });
        }
    });
    dispatch_resume(_timer);
}


@end
